package cn.com.w.redis.use.logic.impl;

import cn.com.w.redis.use.common.enums.CourseEnum;
import cn.com.w.redis.use.domain.UserInfo;
import cn.com.w.redis.use.logic.StudyTimeRankLogc;
import cn.com.w.redis.use.service.IUserInfoService;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.DefaultTypedTuple;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * 学习时间排行榜 逻辑
 */
@Component
@Slf4j
public class StudyTimeRankLogcImpl implements StudyTimeRankLogc {

    @Autowired
    private IUserInfoService iUserInfoService;

    @Autowired
    private RedisTemplate redisTemplate;


    private static List<UserInfo> userInfos = new ArrayList<>();


    /**
     * 获取用户列表
     *
     * @return
     */
    private List<UserInfo> listUserInfos() {
        if (CollectionUtils.isEmpty(userInfos)) {
            userInfos = iUserInfoService.list();
        }
        return userInfos;
    }

    @Override
    public void initCacheBatch() {
        long start = System.currentTimeMillis();
        List<UserInfo> userInfos = listUserInfos();
        if (CollectionUtils.isEmpty(userInfos)) {
            return;
        }
        ZSetOperations zSetOperations = redisTemplate.opsForZSet();

        // 循环每一个日期 yyyy-MM-dd
        getDateStrs().forEach(item -> {
            for (CourseEnum value : CourseEnum.values()) {
                // 输入组
                Set<DefaultTypedTuple<String>> typedTuples = new HashSet<>();
                for (UserInfo userInfo : userInfos) {
                    int expValue = new Random().nextInt(100);
                    DefaultTypedTuple<String> typedTuple = new DefaultTypedTuple<String>(userInfo.getUserName(), (double) expValue);
                    typedTuples.add(typedTuple);
                }
                zSetOperations.add(getRedisKeyForDayRank(value.getCode(), item), typedTuples);

            }
        });

        log.info("initCacheBatch()用时{}毫秒", System.currentTimeMillis() - start);
    }


    @Override
    public String getRedisKeyForDayRank(String courseCode, String dateStr) {
        return "study:time:rank:" + courseCode + ":" + dateStr;
    }

    @Override
    public String getRedisKeyForWeekRank(String courseCode, Integer weekOfYear) {
        return "study:time:rank:" + courseCode + ":" + weekOfYear;
    }

    @Override
    public String getRedisKeyForMonthRank(String courseCode, String month) {
        return "study:time:rank:" + courseCode + ":" + month;
    }

    @Override
    public String getRedisKeyForTotalRank(String courseCode) {
        return "study:time:rank:" + courseCode + ":" + "total";
    }

    private List<String> getDateStrs() {
        List<String> result = new ArrayList<>();
        DateTime begin = DateUtil.beginOfMonth(new Date());
        DateTime end = DateUtil.endOfMonth(new Date());
        // 获取相差天数。
        long betweenDay = DateUtil.between(begin, end, DateUnit.DAY);
        // 第一天
        result.add(DateUtil.format(begin, "yyyy-MM-dd"));
        for (int i = 0; i < betweenDay; i++) {
            DateTime dateTime = DateUtil.offsetDay(begin, 1);
            //System.out.println(DateUtil.format(dateTime, "yyyy-MM-dd"));
            begin = dateTime;
            // 第i+1天
            result.add(DateUtil.format(dateTime, "yyyy-MM-dd"));
        }
        return result;
    }


}
